<?php

declare(strict_types=1);

namespace App\Application\Admin\Controller;

use App\Annotation\Api;
use App\Annotation\View;
use App\Application\Admin\Middleware\AdminMiddleware;
use App\Application\Admin\Model\Package;
use App\Application\Admin\Model\Room;
use App\Application\Admin\Model\Store;
use App\Application\Admin\Service\AdminUserService;
use App\Controller\AbstractController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Application\Admin\Model\Order;
use Hyperf\DbConnection\Db;

#[Middleware(AdminMiddleware::class)]
#[Controller("admin/cleaner_order")]
class CleanerOrderController extends AbstractController
{
    #[View]
    #[GetMapping("index")]
    public function index()
    {

    }

    /*
     * 保洁订单列表
     *
     *
     */
    #[Api]
    #[GetMapping("index/lists")]
    function lists()
    {
        $where = [];
        //门店ID
        $store_id = $this->request->input('store_id', '');
        if ($store_id != '') {
            $where[] = ['store_id', '=', $store_id];
        }
        //订单ID
        $order_on = $this->request->input('order_on', '');
        if ($order_on != '') {
            $where[] = ['order_on', '=', $order_on];
        }
        //用户ID
        $user_id = $this->request->input('user_id', '');
        if ($user_id != '') {
            $where[] = ['user_id', '=', $user_id];
        }
        //开始时间
        $add_time = $this->request->input('add_time', '');
        if ($add_time != '') {
            $where[] = ['created_at', '>', $add_time];
        }
        //结束时间
        $end_time = $this->request->input('end_time', '');
        if ($end_time != '') {
            $where[] = ['created_at', '<', $end_time];
        }
        $isadmin = 0;
        $admin_user = AdminUserService::getInstance()
            ->getAdminUser();
        if ($admin_user['admin_user_id'] !== 1) {
            $store = Store::where('admin_id', $admin_user['admin_user_id'])->select('store_id')
                ->first() ?: [];
            $where[] = ['store_id', '=', $store['store_id']];
        } else {
            $isadmin = 1;
        }
        //提前5分钟发送待打扫保洁订单
        $where[] = ['end_time', '<', time() - 300];
        //订单状态0未开始，1进行中，2已结束，3已退款，4已取消，5已分账，6待付款
        $where[] = ['cleaner_status', '<>', 2];
        $lists = Order::where($where)->whereIn('status', ['2', '3', '5'])
            ->orderBy('order_id', 'DESC')
            ->paginate()->toArray();

        $store_list = Store::where('store_id', '>', 0)
            ->orderBy('created_at', 'DESC')
            ->pluck('store_name', 'store_id');

        $storelist = Store::where('store_id', '>', 0)->pluck('store_name', 'store_id');
        $roomlist = Room::where('room_id', '>', 0)->pluck('name', 'room_id');
        $packagelist = Package::where('package_id', '>', 0)->pluck('name', 'package_id');
        foreach ($lists['data'] as $k => $v) {
            //店铺名称
            $lists['data'][$k]['store_name'] = $storelist[$v['store_id']];
            //包厢名称
            $lists['data'][$k]['room_name'] = $roomlist[$v['room_id']];
            //套餐名称
            $lists['data'][$k]['package_name'] = $packagelist[$v['package_id']];
        }
        return compact('lists', 'store_list', 'isadmin');
    }

    /*
     * 订单开始打扫
     *
     *
     */
    #[Api]
    #[GetMapping("openocleaner")]
    function openocleaner(int $room_id = 0)
    {
        $room_id = $this->request->input('room_id', '');
        $where = [];
        $where[] = ['end_time', '<', time()];
        $where[] = ['room_id', '=', $room_id];
        $where[] = ['cleaner_status', '=', 0];
        Db::table('order')->where($where)->whereIn('status', ['2', '3', '4', '5'])->update([
            'cleaner_status' => 1,
            'updated_at' => date('Y-m-d H:i:s', time())
        ]);
        //房间通电
        $room = Room::where('room_id', $room_id)->select('*')->first() ?: [];
        $res = $this->adminopen($room['type'], $room['door_id'], 30);

        return $res ? $this->returnSuccessJson(compact('res'), '操作成功') : $this->returnErrorJson();
    }

    /*
     * 订单打扫完成
     *
     *
     */
    #[Api]
    #[GetMapping("closeorder")]
    function closeorder(int $order_id = 0)
    {
        $room_id = $this->request->input('room_id', '');
        $where = [];
        $where[] = ['end_time', '<', time()];
        $where[] = ['room_id', '=', $room_id];
        $where[] = ['cleaner_status', '=', 1];
        Db::table('order')->where($where)->whereIn('status', ['2', '3', '4', '5'])->update([
            'cleaner_status' => 2,
            'updated_at' => date('Y-m-d H:i:s', time())
        ]);
        //房间通电
        $room = Room::where('room_id', $room_id)->select('*')->first() ?: [];
        $res = $this->adminopen($room['type'], $room['door_id'], 0);
        return $res ? $this->returnSuccessJson(compact('res'), '操作成功') : $this->returnErrorJson();
    }
}
